<#include "${ftl_skin}/common/header.ftl">
    <link href="${path.skinPath }css/login.css" rel="stylesheet">
    </head>

    <body>
        <div class="ibox-login-ny">
            <div class="form-login">
                <dl class="form-title">
                    <dt><img src="${path.skinPath }img/logo_txt.png" alt=""></dt>
                    <dd>
                        <!--<a href="#"><img src="${path.skinPath }img/logo.png" alt="">--></a>
                    </dd>
                </dl>
                <form class="form-horizontal m-t" id="login_form" method="post">
                    <input type="hidden" name="user_password" id="user_password" value="">
                    <div class="form-ny-box">
                        <ul class="form-list">
                            <li>
                                <input type="text" placeholder="请输入用户名" required name="user_name" id="user_name">
                            </li>
                            <li><input type="text" placeholder="请输入密码" required name="user_password_forge" id="user_password_forge"></li>
                            <#if isClose == false>
                            <li>
                                <img id="randomcode-img" src="${path.servicePath }index/randomcode.do" alt="" width="95" onClick="updateCode();">
                                <input type="text" placeholder="验证码" id="random_code" name="random_code"></li>
                            </#if>
                            <li>
                                <a href="javascript:void();" onClick="javascript:HFLogin();return false;">登录</a>
                            </li>
                        </ul>
                    </div>
                </form>
                <footer>Copy Right © 2017-2018 后台管理系统</footer>
            </div>
        </div>

        <#include "${ftl_skin}/common/js.ftl">
            <script type="text/javascript">
                $(function() {
                    
                    //最顶层窗口显示登录页面
                    if(window.top != window.self) {
                        window.top.location.href = "${path.viewPath }/index/login.do";
                    }
                    
                    //重置登录表单
                    $('#login_form')[0].reset();
                    
                    //按下回车键执行登录
                    $(document).bind("keypress", function() {
                        if(arguments[0].keyCode === 13) {
                            HFLogin();
                        }
                    });

                    //如果有key传过来就跳到找回密码页面
                    <#if key??>
                        showPsDiv(3); 
                    </#if>
                    
                    //获取密码input焦点时，改变其类型为password
                    $("#user_password_forge").focus(function($e){
                       changePwdType(true);
                    });
                    
                });
                
                /**
                 * init页面时密码input类型为text，onfocus时，类型为password，submit时，类型为text
                 * 解决安全扫描软件判断password为敏感控件而导致用户信息被盗窃
                 * 使用修改整段HTML代码方式，而不使用修改type方式，解决ie无法识别type属性，jQuery也无法修改type属性
                 */
                var textHtml = '<input type="text" placeholder="请输入密码" required name="user_password_forge" id="user_password_forge">';
                var pwdHtml = '<input type="password" placeholder="请输入密码" required name="user_password_forge" id="user_password_forge">';
                //改变密码input类型，type为true则为password，否则为text
                function changePwdType(type){
                    var $pwd = $("#user_password");
                    var $pwdForge = $("#user_password_forge");
                    pwdForgeVal = $pwdForge.val();
                    $pwdForge.parent().html(type ? pwdHtml : textHtml);
                    $pwdForge = $("#user_password_forge");
                    $pwdForge.focus();
                    if(!isNull(pwdForgeVal)){
                        $pwd.val(pwdForgeVal);
                        var len = pwdForgeVal.length;
                        var pwdForgeVal = [];
                        for(var i = 0; i < len; i++){
                            pwdForgeVal.push("•");
                        }
                        $pwdForge.val(pwdForgeVal.join(""));
                    }
                }

                //显示登陆框
                function showDivLogin() {
                    $("#div_login").show();
                    $("div[id^=password_]").hide();
                }
                
                //显示找回密码框框
                function showPsDiv(id) {
                    $("#div_login").hide();
                    $("div[id^=password_]").hide();
                    $("#password_" + id).show();

                }
                
                //发送邮件
                function sendmail() {
                    var cemail = $("#cemail").val();
                    console.log("cemail:" + cemail);
                    if(cemail == "") {
                        HFAlert("请输入邮箱！", "", "warning");
                        return;
                    }
                    HFAjax("${path.servicePath }passwordretrieval/sendmail.do", {
                        email: cemail
                    }, function(res) {
                        if(res.state == 0) {
                            HFToastr("操作成功");
                            showPsDiv(2);
                            $("span[name='input_email']").empty().append(cemail);
                            $("#cemail").val("");
                        } else {
                            HFToastrError("操作失败", res.message);
                        }
                    });
                }
                
                //更改密码
                function passwordRetrival() {
                    if($("#password_update").val() != $("#password_check").val()) {
                        HFToastrError("密码不一致");
                        return;
                    }
                    var key = '${key! }';
                    var password = $("#password_update").val();
                    HFAjax("${path.servicePath }passwordretrieval/passwordretrieval.do", {
                        key: key,
                        password: password
                    }, function(res) {
                        if(res.state == 0) {
                            HFToastr("操作成功");
                            showPsDiv(4);
                            window.location.href = "${path.viewPath }index/login.do";
                        } else {
                            HFToastrError("操作失败", res.message);
                        }
                    });
                }
                
                //执行登录
                function HFLogin() {
                    
                    changePwdType(false);

                    if($("#user_name").val() == '') {
                        HFAlert("请输入用户名！", "", "warning");
                        return;
                    }
                    if($("#user_password").val() == '') {
                        HFAlert("请输入密码！", "", "warning");
                        return;
                    }

                    <#if isClose == false>
                    if(!checkCode()) {
                        HFAlert("验证码错误！请重新输入", "", "warning");
                        return;
                    }
                    </#if>

                    showHFLoading();
                    $.ajax({
                        url: "${path.servicePath }/index/login.do",
                        async: true,
                        type: 'POST',
                        data: {
                            j_username: $("#user_name").val(),
                            j_password: hex_md5($("#user_password").val()),
                            random_code: $("#random_code").val()
                        },
                        complete: function(XMLHttpRequest, textStatus) {
                            hideHFLoading();
                        },
                        success: function(msg) {
                            /*if(msg.state == 0) {
                                window.top.location.href = "${path.viewPath }/admin/index.do";
                            } else if(msg.state == 3) {
                                HFAlert("验证码错误！", "", "warning");
                                updateCode();
                            } else {
                                HFAlert("用户名或密码错误！", "", "error", function() {
                                    window.top.location.href = window.top.location.href;
                                });
                            }*/
                            if(msg.state == 0) {
                                window.top.location.href = "${path.viewPath }/admin/index.do";
                            } else  if(msg.state == -5) {
                                HFAlert(msg.message, "", "warning", function () {
                                    window.top.location.href = "${path.viewPath }/admin/index.do";
                                });
                            }  else {
                                changePwdType(true);
                                HFAlert(msg.message, "", "warning");
                                updateCode();
                            }
                        }
                    });
                }
                
                //校验验证码
                function checkCode() {
                    var success = false;
                    $.ajax({
                        url: "${path.servicePath }/index/codeauth.do",
                        async: false,
                        type: 'POST',
                        data: {
                            code: $("#random_code").val()
                        },
                        dataType: "json",
                        success: function(msg) {
                            if(msg.state == 0) {
                                success = true;
                            } else {
                                HFAlert("验证码错误！", "", "warning");
                                updateCode();
                                var pass = $("#user_password").val();
                                changePwdType(true);
                                $("#user_password_forge").val(pass);
                            }
                        }
                    });
                    return success;
                }
                
                //刷新验证码
                function updateCode() {
                    $("#randomcode-img").attr("src", "${path.servicePath }index/randomcode.do?time=" + new Date().getTime());
                }
            </script>

    </body>

    </html>